<?php
class api extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->upload_groupObj = D('upload_group');
		$this->upload_fileObj = D('upload_file');

        // 存储引擎
		$this->config = $this->setting['storage'];
		$this->storage = $this->config['default'];

	}

	
	public function fileList()
    {
		$type = arg('type') ?? 'image';
		$group_id = arg('group_id') ?? 0;
		// 分组列表
		$cond = "group_type='{$type}' and status=1 ";
		$group_list = $this->upload_groupObj->getList($cond);
		// 文件列表
		$cond = "file_type='{$type}' and status=1 ";
		if($group_id){
			$cond .= " and group_id='{$group_id}' ";
		}
		$file_list = $this->upload_fileObj->getlist($cond);
		
		$this->renderSuccess('success', '', compact('group_list', 'file_list'));
		
	}
	
    public function addGroup()
    {
		$group_name = arg('group_name');
		$group_type = arg('group_type') ?? 'image';

		$arrBase = array(
			// 'site_id'=>$this->site['id'],
			'group_name'=>arg('group_name'),
			'group_type'=>arg('group_type') ?? 'image',
		);
		// $data['site_id'] = $this->site['id'];
		$group_id = $this->upload_groupObj->add($arrBase);
        if ($group_id) {
            $this->renderSuccess('添加成功', '', compact('group_id', 'group_name'));
        }
        $this->renderError('添加失败');
    }

    public function editGroup()
    {
		$group_id = arg('group_id');
		$data['group_name'] = arg('group_name');

        $res = $this->upload_groupObj->edit($group_id,$data);
        if ($res) {
            $this->renderSuccess('修改成功');
        }
        $this->renderError('修改失败');
    }

    public function deleteGroup()
    {
		$group_id = arg('group_id');
        $res = $this->upload_groupObj->del($group_id);
        if ($res) {
            $this->renderSuccess('删除成功');
        }
        $this->renderError('删除失败');
    }

    public function deleteFiles()
    {
		$fileIds = arg('fileIds');
        $res = $this->upload_fileObj->softDelete($fileIds,0);
        if ($res) {
            $this->renderSuccess('删除成功');
        }
        $this->renderError('删除失败');
    }

    public function moveFiles()
    {
		$group_id = arg('group_id');
		$fileIds = arg('fileIds');
        $res = $this->upload_fileObj->moveGroup($group_id, $fileIds);
        if ($res) {
            $this->renderSuccess('移动成功');
        }
        $this->renderError('移动失败');
	}
	
	public function upload_image()
    {	
		$group_id = arg('group_id');
		$fileInfo = $_FILES['iFile'];

		$result = $this->upload_fileObj->upload_image($this->config,$fileInfo);
		if($result){
			$this->fileMd5 = $result['fileMd5'];
			$fileDir = $result['fileDir'];
			$fileName = $result['fileName'];
			// 添加文件库记录
			$uploadFile = $this->addUploadFile($group_id, $fileDir, $fileName, $fileInfo, 'image');
			$this->renderSuccess('图片上传成功', '', $uploadFile);
			exit;
		}else{
			$this->renderError('图片上传失败');
		}

    }

    /**
     * 添加文件库上传记录
     * @param $group_id
     * @param $fileName
     * @param $fileInfo
     * @param $fileType
     * @return UploadFile
     */
	private function addUploadFile($group_id, $fileDir, $fileName, $fileInfo, $fileType)
    {
		// $this->config = $this->setting['storage'];
        // 存储引擎
        $storage = $this->config['default'];
        // 存储域名
        $fileUrl = isset($this->config['engine'][$storage]['domain'])
            ? $this->config['engine'][$storage]['domain'].$fileDir.'/' : $fileDir.'/';
		// 添加文件库记录
		$data = [
            'group_id' => $group_id > 0 ? (int)$group_id : 0,
            'storage' => $storage,
            'file_url' => $fileUrl,
            'file_name' => $fileName,
            'file_size' => $fileInfo['size'],
			'file_type' => $fileType,
			// 'file_md5' => $this->fileMd5,
			'extension' => pathinfo($fileInfo['name'], PATHINFO_EXTENSION),
        ];
        $data['file_id'] = $this->upload_fileObj->add($data);
        return $data;
	}
	


}